package com.example.superyi.myteam.myAnswer;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.superyi.myteam.roomPro.entityPro.AuthorEntity;
import com.example.superyi.myteam.R;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;

public class MyAnswerFragment extends Fragment {
    //定义以goodsentity实体类为对象的数据集合
    private ArrayList<AuthorEntity> authorEntities= new ArrayList<>();

    public MyAnswerFragment() {
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //获取fragment的layout
        View view = inflater.inflate(R.layout.fragment_myanswer, container, false);
        initData();
        //对recycleview进行配置
        initRecyclerView(view);

        return view;
    }

    private void initRecyclerView(View view) {
        RecyclerView answerRecyclerview=view.findViewById(R.id.myanswer_recyclerView);
        //创建adapter
        //自定义recyclerveiw的适配器
        MyAnswerRecycleAdapter answerRecycleAdapter=new com.example.superyi.myteam.myAnswer.MyAnswerRecycleAdapter(getActivity(), authorEntities);
        //设置layoutManager,可以设置显示效果，是线性布局、grid布局，还是瀑布流布局
        //参数是：上下文、列表方向（横向还是纵向）、是否倒叙
        answerRecyclerview.setLayoutManager(new GridLayoutManager(getActivity(), 1));

        answerRecyclerview.setAdapter(answerRecycleAdapter);

        answerRecyclerview.addItemDecoration(new DividerItemDecoration(requireActivity(), DividerItemDecoration.VERTICAL));

        answerRecycleAdapter.setOnItemClickListener(new com.example.superyi.myteam.myAnswer.MyAnswerRecycleAdapter.OnItemClickListener() {
            @Override
            public void OnItemClick(View view, AuthorEntity data) {
                //此处进行监听事件的业务处理
                Toast.makeText(getActivity(), "我是item:" + data.getQuestionAuthorName(), Toast.LENGTH_SHORT).show();

            }

            @Override
            public void OnItemLongClick(View view, AuthorEntity data) {
                Toast.makeText(getActivity(), "long click:" + data.getQuestionTitle(), Toast.LENGTH_SHORT).show();
            }
        });
    }

    /**
     * TODO 模拟数据
     */
    private void initData() {
        for (int i=0;i<10;i++){
            AuthorEntity authorEntity=new AuthorEntity();
            authorEntity.setQuestionAuthorName("范闲"+i);
            authorEntity.setReleaseTime(i+"小时前");
            authorEntity.setQuestionTitle("庆国");
            authorEntity.setQuestionContent("这里是第"+i+"条记录的正文部分内容");
            authorEntities.add(authorEntity);
        }
    }
}
